
package com.mephi.controller;


/**
 * Enum class is responsible for storing result.
 * 
 * @author mephisto
 * @since 2011-04-24
 */
public enum GameResult
{
	WHITE_WON("1-0"), BLACK_WON("0-1"), DRAW("0.5-0.5");
	
	/**
	 * 
	 * @param result the result to set, it takes only on of these string of charaters:
	 * 			{"1-0","0-1","0.5-0.5","1/2-1/2"}
	 * @return the result of the game
	 * @throws IllegalArgumentException if result is not one of string of charaters 
	 * 			{"1-0","0-1","0.5-0.5","1/2-1/2"}
	 */
	public static GameResult newInstance(String result)
	{
		if (result.equals(WHITE_WON.toString()))
			return WHITE_WON;
		else if (result.equals(WHITE_WON2))
			return WHITE_WON;
		else if (result.equals(BLACK_WON.toString()))
			return BLACK_WON;
		else if (result.equals(BLACK_WON2))
			return BLACK_WON;
		else if (result.equals(DRAW.toString()))
			return DRAW;
		else if (result.equals(DRAW1_2))
			return DRAW;
		else if (result.equals(DRAW2_1))
			return DRAW;
		else if (result.equals(DRAW2_2))
			return DRAW;
		throw new IllegalArgumentException("Result: " + result + "is invalid!");
	}
	
	public String toString()
	{
		return result;
	}
	
	private GameResult(String result)
	{
		this.result = result;
	}
	
	private static final String DRAW1_2 = "1/2-1/2";
	private static final String DRAW2_1 = "0.5:0.5";
	private static final String DRAW2_2 = "1/2:1/2";
	private static final String WHITE_WON2 = "1:0";
	private static final String BLACK_WON2 = "0:1";
	
	private String result;
}
